package hlhj.fhp.supreme.activitys

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.AppCompatButton
import android.text.Html
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import fhp.hlhj.giantfold.utils.LogUtil
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.customview.datapicker.DPCManager
import hlhj.fhp.supreme.customview.datapicker.DPDecor
import hlhj.fhp.supreme.customview.datapicker.DPMode
import hlhj.fhp.supreme.customview.datapicker.DatePicker
import hlhj.fhp.supreme.javabean.BaseBean
import hlhj.fhp.supreme.javabean.SignListBean
import hlhj.fhp.supreme.javabean.UserInfoBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils
import kotlinx.android.synthetic.main.activity_sign_aty.*
import java.util.*

class SignAty : BaseAty(), DatePicker.OnClickSignIn {
    private lateinit var year:String
    private lateinit var month:String
    private lateinit var startTimeStamp:String
    private lateinit var enTime:String
    private lateinit var day:String
    private val signDatas=ArrayList<String>()
    override fun signIn() {
        //动态更新的时候必须  清除cache
        dpcManager.clearnDATE_CACHE() //清除cache
        //重新设置日期
        val tmp = ArrayList<String>()
        tmp.add("2016-2-20")
        tmp.add("2016-2-21")
        tmp.add("2016-2-22")
        tmp.add("2016-2-25")
        dpcManager.setDecorBG(tmp)

        myDatepicker.setDate(2016, 2)
        myDatepicker.setLeftTitle("2月")
        myDatepicker.setRightTitle(true)

       /* myDatepicker.setDPDecor(object : DPDecor() {
            override fun drawDecorBG(canvas: Canvas, rect: Rect, paint: Paint) {
                paint.color = resources.getColor(R.color.blue)
                canvas.drawCircle(rect.centerX().toFloat(), rect.centerY().toFloat(), rect.width() / 4f, paint)
            }
        })*/
        myDatepicker.invalidate() //刷新
    }
    private lateinit var dpcManager: DPCManager
    override fun getContentId(): Int = R.layout.activity_sign_aty
    override fun initView() {
        val timeMillis = System.currentTimeMillis()
        val ym = MyUtils.timeStampToYM(timeMillis)
        year = ym.split("-")[0]
        month = ym.split("-")[1]
        day=MyUtils.timeStampToD(timeMillis)
        var time = "${year}-${month}-01 00:00:00"
        startTimeStamp = MyUtils.dateToStamp(time)
        enTime=((timeMillis/1000)+(60*60*60*24)).toString()
        setSignList()
        getSignData()
        getPonint()
    }

    private fun getPonint(){
        OkGo.post<UserInfoBean>(Urls.BaseUrl+Urls.UserInfo)
                .execute(object :JsonCallBack<UserInfoBean>(this){
                    override fun onSuccess(response: Response<UserInfoBean>?) {
                        val bean = response?.body()
                        if (bean?.code==1){
                            myPoint.text="我的积分：${bean?.data.score}"
                        }
                    }
                })
    }
    private fun getSignData() {
        OkGo.post<SignListBean>(Urls.BaseUrl + Urls.SIGNList)
                .params("start_time", startTimeStamp)
                .params("end_time", enTime)
                .execute(object : JsonCallBack<SignListBean>(this) {
                    override fun onSuccess(response: Response<SignListBean>?) {
                        val bean = response?.body()
                        if (bean?.code == 1) {
                            if (bean?.data!=null&&bean?.data.size>0){
                                for (i in 0..bean?.data.size - 1) {
                                    val ymd = MyUtils.timeStampToStrYMMD((bean?.data[i].create_time).toLong())
                                    signDatas.add(ymd)
                                }
                                dpcManager.clearnDATE_CACHE() //清除cache
                                dpcManager.setDecorBG(signDatas)
                                myDatepicker.invalidate()

                                if (MyUtils.timeStampToStrYMD((bean?.data[0].create_time).toLong()).equals(MyUtils.timeStampToStrYMD(System.currentTimeMillis()))){
                                    btSign.setText("已签到")
                                    btSign.isClickable=false
                                }else{
                                    btSign.setText("签到")
                                    btSign.setOnClickListener {
                                        OkGo.post<BaseBean>(Urls.BaseUrl+Urls.SIGN)
                                                .execute(object :JsonCallBack<BaseBean>(this@SignAty){
                                                    override fun onSuccess(response: Response<BaseBean>?) {
                                                        val bean = response?.body()
                                                        if (bean?.code==1){
                                                            getSignData()
                                                            getPonint()
                                                        }else{
                                                            MyUtils.toast(bean?.msg)
                                                        }
                                                    }
                                                })
                                    }
                                }
                            }else{
                                btSign.setText("签到")
                                btSign.setOnClickListener {
                                    OkGo.post<BaseBean>(Urls.BaseUrl+Urls.SIGN)
                                            .execute(object :JsonCallBack<BaseBean>(this@SignAty){
                                                override fun onSuccess(response: Response<BaseBean>?) {
                                                    val bean = response?.body()
                                                    if (bean?.code==1){
                                                        getSignData()
                                                    }else{
                                                        MyUtils.toast(bean?.msg)
                                                    }
                                                }
                                            })
                                }
                            }

                        }
                    }
                })
    }

    private fun setSignList() {
        dpcManager = DPCManager.getInstance()
        dpcManager.clearnDATE_CACHE() //清除cache

        //自定义背景绘制示例
        val tmp = ArrayList<String>()
/*        tmp.add("2018-3-4") //yyyy-M-d
        tmp.add("2018-3-5") //yyyy-M-d*/
        dpcManager.setDecorBG(tmp) //预先设置日期背景 一定要在在开始设置
/*        val ym = MyUtils.timeStampToYM(System.currentTimeMillis())
        val year = ym.split("-")[0]
        val month = ym.split("-")[1]*/
        myDatepicker.setDate(year.toInt(), month.toInt()) //设置日期

        myDatepicker.setMode(DPMode.NONE) //设置选择模式
        myDatepicker.setFestivalDisplay(false) //是否显示节日
        myDatepicker.setTodayDisplay(false) //是否高亮显示今天
        myDatepicker.setHolidayDisplay(false) //是否显示假期
        myDatepicker.setDeferredDisplay(false) //是否显示补休
        myDatepicker.setIsScroll(false) //是否允许滑动 false表示左右上下都不能滑动  单项设置上下or左右 你需要自己扩展
        myDatepicker.setIsSelChangeColor(true, resources.getColor(R.color.font_white_one)) //设置选择的日期字体颜色,不然有的背景颜色和默认的字体颜色不搭
        myDatepicker.setDPDecor(object : DPDecor() {
            override fun drawDecorBG(canvas: Canvas, rect: Rect, paint: Paint) {
                paint.color = resources.getColor(R.color.reguler)
                canvas.drawCircle(rect.centerX().toFloat(), rect.centerY().toFloat(), rect.width() / 4f, paint)
            }
        })
        myDatepicker.setLeftTitle("${year}年${month}月${day}日") //左上方text
        myDatepicker.setRightTitle(false) //是否签到
        myDatepicker.setOnClickSignIn(this) //点击签到事件
    }

    override fun initListener() {
        btPointMall.setOnClickListener {
            startActivity(Intent(this,PointMallAty::class.java))
        }
        btExit.setOnClickListener {
            finish()
        }
    }


}
